package org.summer.generator.resolver;

import org.summer.generator.config.CreateConfig;
import org.summer.generator.config.DatabaseConfig;
import org.summer.generator.config.ProjectConfig;
import org.summer.generator.config.WriterConfig;
import org.summer.generator.log.Logger;
import org.summer.generator.log.LoggerManager;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置文件解析器
 */
public class ConfigResolver {
    private static final Logger log = LoggerManager.getLogger(ConfigResolver.class);
    private Properties properties;
    private Properties parseClassLoaderToProperties() throws IOException {
        InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream("generator.properties");
        assert systemResourceAsStream != null;
        Properties p = new Properties();
        p.load(systemResourceAsStream);
        systemResourceAsStream.close();
        return p;
    }
    private void loadConfig(){
        try {
            this.properties = parseClassLoaderToProperties();
            log.debug("解析配置文件信息：" + this.properties)  ;
        } catch (IOException e) {
            log.error("解析配置文件信息出现错误");
            throw new RuntimeException(e);
        }
    }
    public void resolver() {
        this.loadConfig();
    }

    /**
     * 从配置文件解析项目配置
     */
    public ProjectConfig resolverProjectConfig(){
        ProjectConfig config = new ProjectConfig();
        config.setBaseName(this.properties.getProperty("project.base.name"));
        config.setGroupName(this.properties.getProperty("project.group.name"));
        config.setModuleName(this.properties.getProperty("project.module.name"));
        config.setEntityPackageName(this.properties.getProperty("project.entity.package.name","entity"));
        config.setMapperPackageName(this.properties.getProperty("project.mapper.package.name","mapper"));
        config.setRepositoryPackageName(this.properties.getProperty("project.repository.package.name","repository"));
        config.setServicePackageName(this.properties.getProperty("project.service.package.name","service"));
        config.setControllerPackageName(this.properties.getProperty("project.controller.package.name","controller"));
        config.setDtoPackageName(this.properties.getProperty("project.dto.package.name","dto"));
        config.setMapperXmlFilePath(this.properties.getProperty("project.mapper-xml.file-path","mapper"));
        config.setTypescriptTypeFilePath(this.properties.getProperty("project.typescript.type.file-path","ts"));
        config.setTypescriptAntdTableColumnsFilePath(this.properties.getProperty("project.typescript.antd.table-columns.file-path","columns"));
        config.setTypescriptReduxReducerFilePath(this.properties.getProperty("project.typescript.redux.reducer.file-path","reducer"));
        log.debug("解析项目配置信息： " + config);
        return config;
    }

    /**
     * 解析文件写入操作配置
     */
    public WriterConfig resolverWriterConfig(){
        WriterConfig config = new WriterConfig();
        config.setRequestPrefix(getProps("writer.request.prefix",""));
        config.setSqlSelectIgnoreFields(getProps("writer.sql.select.ignore.fields","").split(","));
        config.setSqlUpdateIgnoreFields(getProps("writer.sql.update.ignore.fields","").split(","));
        config.setSqlLogicallyDelete(getProps("writer.sql.logically-delete.field"));
        log.debug("解析文件写入配置： " + config);
        return config;
    }

    /**
     * 从配置文件解析数据库配置
     */
    public DatabaseConfig resolverDatabaseConfig(){
        DatabaseConfig config = new DatabaseConfig();
        config.setUsername(properties.getProperty("datasource.username","root"));
        config.setPassword(properties.getProperty("datasource.password","123456"));
        config.setHost(properties.getProperty("datasource.host","localhost"));
        config.setPort(properties.getProperty("datasource.port","3306"));
        config.setDriverClassName(properties.getProperty("datasource.driver-class-name","com.mysql.cj.jdbc.Driver"));
        config.setDatabaseName(properties.getProperty("datasource.database-name"));
        log.debug("解析数据库配置： " + config);
        return config;
    }

    /**
     * 解析文件创建配置
     */
    public CreateConfig resolverCreateConfig(){
        CreateConfig config = new CreateConfig();
        config.setAll(getBooleanProps("crate.all.file"));
        config.setCreateDir(getBooleanProps("create.dir"));
        config.setCreateEntityFile(getBooleanProps("create.entity.file"));
        config.setCreateMapperFile(getBooleanProps("create.mapper.file"));
        config.setCreateMapperFile(getBooleanProps("create.mapper.xml.file"));
        config.setCreateRepositoryFile(getBooleanProps("create.repository.file"));
        config.setCreateServiceFile(getBooleanProps("create.service.file"));
        config.setCreateControllerFile(getBooleanProps("create.controller.file"));
        config.setCreateDtoFile(getBooleanProps("create.dto.file"));
        config.setCreateTypescriptTypeFile(getBooleanProps("create.typescript.type.file"));
        config.setCreateTypescriptAntdTableColumnsFile(getBooleanProps("create.typescript.antd.table-columns.file"));
        config.setCreateTypescriptReduxReducerFile(getBooleanProps("create.typescript.redux.reducer.file"));
        log.debug("解析文件创建配置： " + config);
        return config;
    }

    /**
     * 从配置文件中获取布尔属性
     * @param key 属性值
     */
    private boolean getBooleanProps(String key){
        return Boolean.parseBoolean(getProps(key,"false"));
    }

    /**
     * 获取属性辅助方你发
     * @param key 属性key
     */
    private String getProps(String key){
        return this.properties.getProperty(key);
    }
    /**
     * 获取属性辅助方你发
     * @param key 属性key
     * @param defaultValue 默认值
     */
    private String getProps(String key, String defaultValue){
        return this.properties.getProperty(key,defaultValue);
    }
}
